package com.fenghongzhang.day011;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.Telephony;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:"+110));
                startActivity(intent);

            }
        });

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS,Manifest.permission.WRITE_CONTACTS,Manifest.permission.READ_SMS,Manifest.permission.CALL_PHONE},100);
        }else{
            info();
        }
    }

    private void info() {
        //同意
        //解析者
        ContentResolver contentResolver = getContentResolver();
        Cursor query = contentResolver.query(Telephony.Sms.CONTENT_URI, null, null, null, null);
        if (query != null) {
            while (query.moveToNext()) {
                String name = query.getString(query.getColumnIndex(Telephony.Sms.ADDRESS));
                String number = query.getString(query.getColumnIndex(Telephony.Sms.BODY));
                Toast.makeText(this, number + name, Toast.LENGTH_SHORT).show();


            }
            query.close();
            /**
             * 定义类.去接收所以差到的值.
             * 把这个类,装到集中
             *
             * 打开数据库
             * db.bt;
             * db.insert()
             * db.set
             * db.endT
             *
             */

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            //同意
            //解析者
            info();
        }else{
            //不同意
            finish();
        }
    }
}
